package com.it.gw.modules.water.fragment

import LoginVo
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.it.common.utils.DateUtils
import com.it.common.utils.DateUtils.DAY_DATE_FORMATTER
import com.it.data.manager.AppCacheManager
import com.it.data.network.rsp.RsvrDetailVo
import com.it.gw.base.BaseFragment
import com.it.gw.databinding.FragmentReservoirSeeBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.water.activity.ReservoirUploadActivity
import com.it.gw.modules.water.adapter.ReservosirseeAdapter
import com.tencent.mmkv.MMKV
import java.util.Date


class ReservoirSeeFragment : BaseFragment<FragmentReservoirSeeBinding>() {
    val datas = mutableListOf<RsvrDetailVo>()
    val adapter by lazy { ReservosirseeAdapter(context = requireActivity(), datas = datas) }


    companion object {
        const val TAG = "ReservoirSeeFragment"
    }

    override fun initObserver() {
        (requireActivity() as ReservoirUploadActivity).waterViewModel.rsvrDetailVoLiveData.observe(
            this
        ) {
            ProgressDialog.hide()
            datas.clear()
            if (it.result && it.data != null) {
                datas.addAll(it.data!!)
                Log.d(TAG, "initObserver: ${it.data}")
            }
            adapter.notifyDataSetChanged()

        }

    }

    override fun initListener() {

        binding.date.setOnClickListener {
            (requireActivity() as ReservoirUploadActivity).showDatePickerDialog(selectDateAction = {
                binding.date.text = it
                ProgressDialog.show(requireActivity())
                getRsvrByAddvcdAndTm_APP()
            })

        }
        binding.rlSearch.setOnClickListener {
            ProgressDialog.show(requireActivity())
            getRsvrByAddvcdAndTm_APP()
        }

    }


    override fun getView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentReservoirSeeBinding.inflate(layoutInflater)
        return binding.root
    }

    fun searchView() {
        if (binding.llSearch.visibility == View.VISIBLE) {
            binding.llSearch.visibility = View.GONE
        } else {
            binding.llSearch.visibility = View.VISIBLE
        }

    }

    override fun initData() {
        binding.recyclerView.adapter = adapter
        adapter.notifyDataSetChanged()
        getRsvrByAddvcdAndTm_APP()

    }

    fun getRsvrByAddvcdAndTm_APP() {
        val depId = MMKV.defaultMMKV()
            .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)?.depId
        val id = MMKV.defaultMMKV()
            .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)?.id

        depId?.let {
            (requireActivity() as ReservoirUploadActivity).waterViewModel.getRsvrByAddvcdAndTm_APP(
                masterCd = "$id",
                addvcd = depId,
                tm = binding.date.text.toString(),
            )
        }
    }

    override fun initView() {
        binding.date.text = DateUtils.dateToString(DAY_DATE_FORMATTER, Date())
    }


}